#include "lcfg/LocalConfigTransfer.h"


namespace ecarx::transfer::lcfg {

class LocalConfigTransfer::LocalConfigTransferInternal {
  private:
    NetState mState;
    NetStateCallback mStateCallback;

  public:
    LocalConfigTransferInternal() : mState(NetState::INIT) {}
    // 打开链接
    bool open() {
        notifyNetState(NetState::Ready);
        return true;
    }
    // 关闭链接
    bool close() {
        notifyNetState(NetState::Close);
        return true;
    }

    bool write(const Signal&) {
      return false;
    }
    const std::optional<Signal> read([[maybe_unused]]const int32_t signalId) const {
      std::optional<Signal> res;
      return res;
    }

    NetState getState() { return mState; }

    void registerStateHandler(NetStateCallback&& _callback) { mStateCallback = _callback; }

  private:
    void notifyNetState(NetState state) {
        mState = state;
        if (mStateCallback != nullptr) {
            mStateCallback(state);
        }
    }
};

LocalConfigTransfer::LocalConfigTransfer() : mImpl(new LocalConfigTransferInternal()) {}

LocalConfigTransfer::~LocalConfigTransfer() {
    delete mImpl;
}

// 打开链接
bool LocalConfigTransfer::open() {
    return mImpl->open();
}
// 关闭链接
bool LocalConfigTransfer::close() {
    return mImpl->close();
}

bool LocalConfigTransfer::write(const Signal&) {
  return false;
}
const std::optional<Signal> LocalConfigTransfer::read(const int32_t signalId) const {
  return mImpl->read(signalId);
}

NetState LocalConfigTransfer::getState() {
    return mImpl->getState();
}

void LocalConfigTransfer::registerStateHandler(NetStateCallback&& _callback) {
    mImpl->registerStateHandler(std::move(_callback));
}

void LocalConfigTransfer::onSignalChanged([[maybe_unused]] SignalChangeCallback&& callback) {}

}  // namespace ecarx::transfer::lcfg
